﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using Project.module;
namespace Project
{
    public partial class Form1 : Form
    {
        public class ComboboxItem
        {
            public string Text { get; set; }
            public int Value { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }
        private BanDTO banchon=null;
        public MonAnDTO monchon = null;
        public MonAnDTO edit = null;
        public int kvmanager = 0;
        private BanDTO banmanager = null;
        private LoaimonDTO loaimanager = null;
        private float tongtien = 0;
        private bool hoadon = false;
        int dbchon = 0;
        int indexban = 0;
        public Form1()
        {
            InitializeComponent();
        }
        
        private void Loaddata()
        {
            List<KhuVucDTO> lstKV = KhuVucBUS.Listkhuvuc();
            lsvKhuvuc.LargeImageList = imgKV;
            if (lstKV != null)
            {
                foreach (KhuVucDTO kv in lstKV)
                {
                    ListViewItem itm = new ListViewItem();

                    itm.Text = kv.Tenkv;
                    
                    itm.ImageIndex = 0;
                    itm.Tag = kv.Makv;
                    //kv_lstKV.Items.Add(itm);
                    lsvKhuvuc.Items.Add(itm);
                    
                    
                }
                lsvKhuvuc.Items[0].Selected = true;

                
            }
            List<LoaimonDTO> lstloaimon = LoaiMonBUS.Listloaimon();
            if (lstloaimon != null)
            {
                foreach (LoaimonDTO loai in lstloaimon)
                {
                    ComboboxItem itm = new ComboboxItem();
                    itm.Text = loai.Tenloai ;
                    itm.Value = loai.Maloai;
                    cbLoaimon.Items.Add(itm);
                }
            }
            cbLoaimon.SelectedIndex = cbLoaimon.FindStringExact("Bình thường");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtMon.AutoGenerateColumns = false;
            dtDatmon.AutoGenerateColumns = false;
            mon_dgvMon.AutoGenerateColumns = false;
            bntDatmon.Visible = false;
            Loaddata();
            
            
        }
         
        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbLoaimon.SelectedIndex >= 0)
            {
                dtMon.DataSource = null;
                int chon = (cbLoaimon.SelectedItem as ComboboxItem).Value;
                dtMon.DataSource = MonAnBUS.Danhsachmon(chon);
                
            }
        }

        private void labelX10_Click(object sender, EventArgs e)
        {

        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            if (hoadon==true)
            {
                printDialog1.ShowDialog();
                MessageBox.Show("Giả lập in thành công !");
                HoaDonDTO hd = new HoaDonDTO();
                hd.Madatmon = DatMonBUS.GetMaDM(banchon.Maban);
                hd.Ngaylap = DateTime.Now;
                hd.Nguoilap = 1;
                hd.Tongtien = tongtien;
                int mahd = HoaDonBUS.ThemHoaDon(hd);
                int madm = DatMonBUS.GetMaDM(banchon.Maban);
                if (mahd > 0)
                {
                    Taohoadon(mahd,madm);
                    DatMonBUS.Thanhtoan(madm);
                }
                Loadbanan(banchon.Maban,lsvBan);
                hoadon = false;
            }
            else
            {
                MessageBox.Show("Hoá đơn chưa tồn tại để in !");
            }
            
        }
        private void Taohoadon(int mahd,int madm)
        {
            
            DataTable dt = CTDatmonBUS.GetCTDatmon(madm);
            if (dt != null)
            {
                
                foreach (DataRow dr in dt.Rows)
                {
                    CTHoaDonDTO ct = new CTHoaDonDTO();
                    ct.Mactdm = int.Parse(dr["MACTDM"].ToString());
                    ct.Mahd = mahd;
                    ct.Soluong = int.Parse(dr["SOLUONG"].ToString());
                    ct.Dongia = float.Parse(dr["DONGIA"].ToString());
                    try
                    {
                        CTHoaDonBUS.ThemCT(ct);
                        BanBUS.SetTrangThai(banchon.Maban, 0);
                        
                    }
                    catch (InvalidCastException e)
                    {
                        throw (e);
                    }
                    
                }

            }
        }

        private void lsvKhuvuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            lsvBan.Items.Clear();
            if (lsvKhuvuc.SelectedItems.Count > 0)
            {
                
                int makv = int.Parse(lsvKhuvuc.SelectedItems[0].Tag.ToString());
                Loadbanan(makv,lsvBan);
            }
        }
        private void Loadbanan(int makv,ListView lv)
        {
            List<BanDTO> lstban = BanBUS.Listbanan(makv);
            lv.Items.Clear();
            lv.LargeImageList = imgBan;
            if (lstban != null)
            {
                foreach (BanDTO ban in lstban)
                {
                    ListViewItem itm = new ListViewItem();
                    if (ban.Trangthai == 0)
                        itm.ImageIndex = 1;
                    else
                    itm.ImageIndex = 0;
                    itm.Text = "#" + ban.Maban;
                    itm.Tag = ban;
                    lv.Items.Add(itm);
                }
            }
            if (banchon != null)
            {
                lsvBan.Items[indexban].Selected = true;
            }

           
        }

        private void lsvBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            tongtien = 0;
            dtDatmon.DataSource = null;
            if (lsvBan.SelectedItems.Count == 0) return;
            banchon = (BanDTO)lsvBan.SelectedItems[0].Tag;
            Thongtinban(banchon);
            LoadResult();



        }
        private void Thongtinban(BanDTO ban)
        {
            if (ban.Trangthai == 1)
            {
                lblStatusb.Text = "Đang phục vụ";
                Loaddatmon(ban.Maban);
                
            }
            else if (ban.Trangthai == 0)
                lblStatusb.Text = "Trống";
            else lblStatusb.Text = "Đã đặt";
            lblMaban.Text = ban.Maban.ToString();

        }
        private void Loaddatmon(int ban)
        {
            int madm=DatMonBUS.GetMaDM(ban);
            DataTable dt = CTDatmonBUS.GetCTDatmon(madm);
            if (dt != null)
            {
                dtDatmon.DataSource = dt;
                foreach(DataRow dr in dt.Rows)
                {
                    tongtien += float.Parse(dr["THANHTIEN"].ToString());
                }
                
            }
            
        }
        private void LoadResult()
        {
            txtTongtien.Text = string.Format("{0:0,0 VND}", tongtien);
            txtTienthua.Text = "";
            textBoxX2.Text = "";
            lblHoadon.Text = lsvKhuvuc.SelectedItems[0].Text + "- Bàn " + banchon.Maban;
        }

        private void labelX25_Click(object sender, EventArgs e)
        {

        }

        private void dtMon_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dtMon.SelectedRows.Count > 0)
            {
                bntDatmon.Visible = true;
                int index = e.RowIndex;
                DataGridViewRow dr = dtMon.Rows[index];
                monchon = new MonAnDTO();
                monchon.Mamon=int.Parse(dr.Cells["MAMONAN"].Value.ToString());
                monchon.Tenmon=dr.Cells["TENMON"].Value.ToString();
                monchon.Dongia=float.Parse(dr.Cells["DONGIA"].Value.ToString());
                
            }
        }

        private void bntDatmon_Click(object sender, EventArgs e)
        {
            if (monchon != null && banchon != null)
            {
                frmThemmon frm = new frmThemmon();
               
                frm.Laymon = monchon;
                frm.Tag = banchon;
                
                frm.ShowDialog();
                LoadResult();
                Loadbanan(int.Parse(lsvKhuvuc.SelectedItems[0].Tag.ToString()), lsvBan);
                
                Loaddatmon(banchon.Maban);
                
                
                monchon = null;
                //banchon = null;
            }
            if (banchon == null)
            {
                MessageBox.Show("Chưa chọn bàn để đặt món!");
            }
        }

        private void textBoxX2_TextChanged(object sender, EventArgs e)
        {
            float number;

            if (float.TryParse(textBoxX2.Text, out number) == true && float.Parse(textBoxX2.Text) > 0)
            {
                float tienthua=(float.Parse(textBoxX2.Text) - tongtien);
                
                txtTienthua.Text = string.Format("{0:0,0 VND}",tienthua );
                if (tienthua > 0)
                    hoadon = true;
                else
                    hoadon = false;
            }
            
            

                
        }
        // Tab quan ly khu vuc
        private void kv_lstKV_SelectedIndexChanged(object sender, EventArgs e)
        {
            kv_lsvBan.Items.Clear();
            if (kv_lstKV.SelectedItems.Count > 0)
            {

               kvmanager = int.Parse(kv_lstKV.SelectedItems[0].Tag.ToString());
                Loadbanan(kvmanager, kv_lsvBan);
            }
        }

        private void kv_lsvBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (kv_lsvBan.SelectedItems.Count > 0)
            {
                banmanager = (BanDTO)kv_lsvBan.SelectedItems[0].Tag;
                kv_lblSoban.Text = banmanager.Maban.ToString();
                kv_txtSL.Text = banmanager.Succhua.ToString();
                kv_lblKhuvuc.Text = kvmanager.ToString();
                if (banmanager.Trangthai == 1)
                    kv_lblTrangthai.Text = "Đang phục vụ";
                else
                    kv_lblTrangthai.Text = "Trống";
                
            }
        }

        private void kv_bntReset_Click(object sender, EventArgs e)
        {

        }

        private void kv_bntUpdate_Click(object sender, EventArgs e)
        {
            bool kt = BanBUS.Update(banmanager.Maban, int.Parse(kv_txtSL.Text), 0, -1);
            if (kt == true)
                MessageBox.Show("Cập nhật thành công !");
            Loadbanan(kvmanager, kv_lsvBan);
        }

        private void kv_bntDichuyen_Click(object sender, EventArgs e)
        {
            frmDichuyenban frm = new frmDichuyenban();
            frm.Tag = banmanager;
            frm.ShowDialog();
            Loadbanan(kvmanager, kv_lsvBan);

            
        }

        private void kv_bntThemban_Click(object sender,EventArgs e)
        {
            frmThemban frm = new frmThemban();
            frm.Tag = kvmanager;
            if (KhuVucBUS.CheckFull(kvmanager) == false)
            {
                frm.ShowDialog();
                Loadbanan(kvmanager, kv_lsvBan);
            }
            MessageBox.Show("Full ! Không thể thêm bàn !");
        }

      
        //Su kien cua tab quan ly khu vuc
        private void tabControl1_SelectedTabChanged(object sender, DevComponents.DotNetBar.TabStripTabChangedEventArgs e)
        {
            
            
        }
        
        private void LoadQuanLyKhuVuc()
        {
            Loaddatban();
            lblTime.Text = DateTime.Now.ToShortTimeString();
            kv_lstKV.Items.Clear();
            List<KhuVucDTO> lstKV = KhuVucBUS.Listkhuvuc();
            kv_lstKV.LargeImageList = imgKV;
            if (lstKV != null)
            {
                foreach (KhuVucDTO kv in lstKV)
                {
                    ListViewItem itm = new ListViewItem();

                    itm.Text = kv.Tenkv;

                    itm.ImageIndex = 0;
                    itm.Tag = kv.Makv;
                    kv_lstKV.Items.Add(itm);



                }
                kv_lstKV.Items[0].Selected = true;
            }

        }
        private void Loaddatban()
        {
            List<DatBanDTO> lst = DatBanBUS.ListDatBan();
            kv_dgvDatban.DataSource = lst;
        }

        private void kv_bntXoaban_Click(object sender, EventArgs e)
        {
            if (kv_lsvBan.SelectedItems.Count > 0)
            {
                BanDTO ban = (BanDTO)kv_lsvBan.SelectedItems[0].Tag;
                if (ban.Trangthai == 0)
                {
                    if (MessageBox.Show("Bạn chắn chắn rằng muốn xóa dữ liệu ?", " Xoá bàn ......", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        BanBUS.Xoaban(banmanager);
                        Loadbanan(kvmanager, kv_lsvBan);
                    }
                }
                else
                {
                    MessageBox.Show("Bàn đang phục vụ không thể xoá !","Không thể xoá", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_MouseEnter(object sender, EventArgs e)
        {
            

        }


        private void tabItem3_Click(object sender, EventArgs e)
        {
            LoadQuanLyKhuVuc();
        }
        private void buttonX13_Click(object sender, EventArgs e)
        {
            if (kv_lstKV.SelectedItems.Count > 0)
            {
                int kv = int.Parse(kv_lstKV.SelectedItems[0].Tag.ToString());
                if (KhuVucBUS.CheckNull(kv) == true)
                {
                    if (MessageBox.Show("Bạn chắn chắn rằng muốn xóa dữ liệu ?", " Xoá khu vực ......", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        KhuVucBUS.Delete(kv);
                        LoadQuanLyKhuVuc();
                    }
                }
                else
                {
                    MessageBox.Show("Tồn tại bàn ! Không thể xoá !");
                }
            }
        }
        // Sự kiện tab quản lý món ăn
        private void tabControl1_Click(object sender, EventArgs e)
        {
            
        }
        private void LoadQuanLyMonAn()
        {
            mon_lsvDanhmuc.Items.Clear();
            mon_lsvDanhmuc.LargeImageList = imgMon;

            List<LoaimonDTO> lst = LoaiMonBUS.Listloaimon();
            if (lst != null)
            {
                foreach (LoaimonDTO loai in lst)
                {
                    ListViewItem itm = new ListViewItem();
                    itm.ImageIndex = 0;
                    itm.Text = loai.Tenloai;
                    itm.Tag = loai.Maloai;
                    mon_lsvDanhmuc.Items.Add(itm);
                }
            }

        }

        private void tabItem4_Click(object sender, EventArgs e)
        {
            LoadQuanLyMonAn();
        }

        private void mon_bnt_Click(object sender, EventArgs e)
        {
            frmUpdatemon frm = new frmUpdatemon();
            frm.Tag = edit;
            frm.ShowDialog();
            LoadMonManager(loaimanager.Maloai);
        }

        private void mon_lsvDanhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (mon_lsvDanhmuc.SelectedItems.Count > 0)
            {

                loaimanager = new LoaimonDTO();
                loaimanager.Maloai = int.Parse(mon_lsvDanhmuc.SelectedItems[0].Tag.ToString());
                loaimanager.Tenloai = mon_lsvDanhmuc.SelectedItems[0].Text;
                LoadMonManager(loaimanager.Maloai);

                
            }
        }
        private void LoadMonManager(int maloai)
        {
            mon_dgvMon.DataSource = null;
            mon_dgvMon.DataSource = MonAnBUS.Danhsachmon(maloai);
        }
        private void mon_bntThemmon_Click(object sender, EventArgs e)
        {
            frmAddnewmon frm = new frmAddnewmon();
            frm.Tag = loaimanager;
            frm.ShowDialog();
            LoadMonManager(loaimanager.Maloai);
        }

        private void mon_dgvMon_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void mon_dgvMon_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (mon_dgvMon.SelectedRows.Count > 0)
            {
                
                int index = e.RowIndex;
                DataGridViewRow dr = mon_dgvMon.Rows[index];
                edit = new MonAnDTO();
                edit.Mamon = int.Parse(dr.Cells["MAMON"].Value.ToString());
                edit.Tenmon = dr.Cells["TEN"].Value.ToString();
                edit.Dongia = float.Parse(dr.Cells["GIA"].Value.ToString());
                edit.Maloai=int.Parse(dr.Cells["MALOAI"].Value.ToString());
                edit.Madv = int.Parse(dr.Cells["MADONVI"].Value.ToString());


            }
        }

        private void mon_bntXoamon_Click(object sender, EventArgs e)
        {
            if (mon_dgvMon.SelectedRows.Count >= 0)
            {
                if (MessageBox.Show("Bạn chắn chắn rằng muốn xóa dữ liệu ?", " Xoá món ......", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MonAnBUS.Xoamon(edit.Mamon);
                    mon_dgvMon.Refresh();
                }
                LoadMonManager(loaimanager.Maloai);
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void tabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11)
            {

                Goimon();
            }
        }
        private void Goimon()
        {
            if (monchon != null && banchon != null)
            {
                frmThemmon frm = new frmThemmon();

                frm.Laymon = monchon;
                frm.Tag = banchon;

                frm.ShowDialog();
                Loaddatmon(banchon.Maban);
                Loadbanan(int.Parse(lsvKhuvuc.SelectedItems[0].Tag.ToString()), lsvBan);
                //monchon = null;
                //banchon = null;
            }
            else
            {
                MessageBox.Show("Vui lòng chọn món !");
            }
        }

        private void dtMon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                Goimon();
            }
        }

        private void lsvBan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                Goimon();
            }
        }

        private void kv_bntDatban_Click(object sender, EventArgs e)
        {
            frmDatban frm = new frmDatban();
            frm.ShowDialog();
            Loaddatban();
        }

        private void kv_dgvDatban_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonX14_Click(object sender, EventArgs e)
        {
            if (kv_dgvDatban.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn chắn chắn rằng muốn huỷ đặt bàn ?", " Huỷ đặt bàn ......", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (DatBanBUS.Huy(dbchon))
                    {
                        MessageBox.Show("Huỷ thành công !");
                        Loaddatban();
                        dbchon = 0;
                    }
                }
                

            }
        }

        private void kv_dgvDatban_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (kv_dgvDatban.SelectedRows.Count > 0)
            {
                int index = e.RowIndex;
                DataGridViewRow dr = kv_dgvDatban.Rows[index];
                dbchon = int.Parse(dr.Cells["datbanCl1"].Value.ToString());
            }
        }
        
        //báo cáo thống kê
        private void tabItem2_Click(object sender, EventArgs e)
        {
            LoadHoaDon();
        }
        private void LoadHoaDon()
        {
            List<HoaDonDTO> lst = HoaDonBUS.ListHD();
            grcHoaDon.DataSource = lst;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            grcHoaDon.ShowPrintPreview();
        }

        private void grcHoaDon_Click(object sender, EventArgs e)
        {
            int mahd = int.Parse(grvhoadon.GetFocusedRowCellDisplayText(Mahd).ToString());
            grcChitiet.DataSource = CTHoaDonBUS.GetChiTietDM(mahd);
        }
        private void LoadChiTietHD(int mahd)
        {
            
        }

        private void lsvBan_Click(object sender, EventArgs e)
        {
            if (lsvBan.SelectedItems.Count > 0)
            {

                indexban = lsvBan.SelectedIndices[0];
                
                
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát ?", " Thoát ......", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void dtMon_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (monchon != null && banchon != null)
            {
                frmThemmon frm = new frmThemmon();

                frm.Laymon = monchon;
                frm.Tag = banchon;

                frm.ShowDialog();
                LoadResult();
                Loadbanan(int.Parse(lsvKhuvuc.SelectedItems[0].Tag.ToString()), lsvBan);

                Loaddatmon(banchon.Maban);


                monchon = null;
                //banchon = null;
            }
            if (banchon == null)
                MessageBox.Show("Chưa chọn bàn!!!");
        }

        
    }
}
